<?php
@ob_start();
@session_start();
error_reporting (E_ALL ^ E_NOTICE);
require_once("../../includes/GPSFunction.php");
require_once("../../Mail/class.phpmailer.php");
//	DB Connection
require("../../includes/config.inc.php"); 
require("../../includes/Database.class.php"); 

$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 
$db->connect(); 


function sendSMTPMail($to,$subject,$message,$fromName,$host='',$uname='',$pass='',$fromEmail='')
{
	$mail = new PHPMailer();
	$mail->IsSMTP(); // send via SMTP

	if($host == '')
		$mail->Host     = "mail.shastrasoftech.com"; // SMTP servers
	else
		$mail->Host     = $host; // SMTP servers
		
	$mail->SMTPAuth = true;     // turn on SMTP authentication
	
	if($uname == '')
		$mail->Username = "gpstrack@shastrasoftech.com";  // SMTP username
	else
		$mail->Username = $uname;  // SMTP username
	
	if($pass == '')
		$mail->Password = "ssgpstrack123 "; // SMTP password
	else
		$mail->Password = $pass; // SMTP password

	$mail->AddAddress($to);
	
	if($fromEmail == '')
		$mail->From     = "gpstrack@shastrasoftech.com";
	else
		$mail->From     = $fromEmail;
		
	if($fromName!='')
		 $mail->FromName = $fromName." Support";
	else $mail->FromName = "Support";
	
	$mail->WordWrap = 50; 
	$mail->IsHTML(true);
	
	$mail->Subject = $subject;			
	$mail->Body = $message;
	
	if($mail->Send())
	{
		return 1;	
	}
	else
	{
		return 0;	
	}
}


if($_GET[ajaxQry] !='' && $_GET[ajaxQry] !='')
{
	$rows = $db->query($_GET[ajaxQry]);
	echo $db->affected_rows;
}
if($_POST[txtUserId] != '' && $_POST[txtClientId] != '')
{
	echo '<span style="color:red;">Redirecting...</span>';
	$_SESSION[userID] = $_POST[txtUserId];
	$_SESSION[clientID] = $_POST[txtClientId];

	header("location:../../modules/user/");
	exit;
}
if(isset($_GET[txtSmtpHost]) && $_GET[txtSmtpHost]!='' && isset($_GET[txtSmtpUser]) && $_GET[txtSmtpUser]!='' && isset($_GET[txtSmtpPass]) && $_GET[txtSmtpPass]!='')
{
	//print_r($_GET);
	$to = $_GET[mailId];
	$sub = " Test Mail ".$_GET[txtClientName];
	$fr = $_GET[txtClientName];
 	$message = "SMTP Configuration tested successfully.";
	
	if(sendSMTPMail($to,$sub,$message,$_GET[txtClientName],$_GET[txtSmtpHost],$_GET[txtSmtpUser],$_GET[txtSmtpPass],$_GET[mailId]))
	{
		echo 1;
	}
	else
	{
		echo 0;
	}
}


if(isset($_GET[modeltype]) && $_GET[modeltype]!='')
{
	if($_GET[modeltype] == 'add')
	{
		$chkCont = "select * from tb_device_model where model = '".$_GET[modelName]."' AND tdm_manuName ='".$_GET[modelManu]."' AND tdm_aliasName = '".$_GET[modelAlias]."' AND tdm_noofInputs =".$_GET[modelInputs]." AND tdm_noofOutputs =".$_GET[modelOutputs];
		$resCont = $db->query($chkCont);
		if($db->affected_rows == 0)
		{
			$cdata["model"] = $_GET[modelName];
			$cdata["tdm_manuName"] = $_GET[modelManu];
			$cdata["tdm_aliasName"] = $_GET[modelAlias];
			$cdata["tdm_noofInputs"] = $_GET[modelInputs];
			$cdata["tdm_noofOutputs"] = $_GET[modelOutputs];
			$cdata["tdm_fuelFunc"] = $_GET[fuelFunc];
			$cdata["tdm_immobFunc"] = $_GET[immobFunc];
		
			//print_r($cdata); exit;
			if($db->query_insert("tb_device_model", $cdata))
			{
				echo 1;
			}
			else
			{
				echo 0;
			}
		}
		else echo 2;
	}
	else
	{
		$cdata["model"] = $_GET[modelName];
		$cdata["tdm_manuName"] = $_GET[modelManu];
		$cdata["tdm_aliasName"] = $_GET[modelAlias];
		$cdata["tdm_noofInputs"] = $_GET[modelInputs];
		$cdata["tdm_noofOutputs"] = $_GET[modelOutputs];
		$cdata["tdm_fuelFunc"] = $_GET[fuelFunc];
		$cdata["tdm_immobFunc"] = $_GET[immobFunc];
		
		//print_r($cdata);exit;
		
		if($db->query_update("tb_device_model", $cdata, "id = ".$_GET[cid]))
			echo 3;
		else echo 4;
	}
}


?>